package com.shopoo.oauth.domain.service;

import java.util.Map;

import com.google.gson.Gson;
import com.nimbusds.jose.JWSObject;
import com.shopoo.exception.BizException;
import com.shopoo.oauth.domain.constants.AuthConstant;
import com.shopoo.oauth.domain.entity.User;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Component;

/**
 *
 * @Author <a href="mailto:android_li@sina.cn">MaoYuan.Li</a>
 * @Date 2022/3/17 12:37 AM
 */
@Component
public class UserDomainService {

	/**
	 * 根据token获取用户信息
	 * @param token
	 * @Return com.shopoo.oauth.domain.entity.User
	 * @Date: 2022/3/17 12:39 AM
	 * @Author: <a href="mailto:android_li@sina.cn">MaoYuan.Li</a>
	 */
	@SneakyThrows
	public User getUserByToken(String token) {
		String realToken = token.replace(AuthConstant.JWT_TOKEN_PREFIX, "");
		JWSObject jwsObject = JWSObject.parse(realToken);
		Map<String, String> map = new Gson().fromJson(jwsObject.getPayload().toString(), Map.class);
		User user = new User();
		user.setUserId(map.get(AuthConstant.USERID));
		user.setUsername(map.get(AuthConstant.USERNAME));
		return user;
	}

}
